<?php
class articleModel extends Model{
	private $tb = 'article';

	public function __construct(){
		parent::__construct($this->tb);
	}

	private function _valid( &$data ){
		if( empty($data['title']) || empty($data['category']) || empty($data['content']) ){
			return $this->error( lang( array('required_fields','required') ) );	
		}elseif( !valid_str_len($data['title'],120) ){
			return $this->error( lang( array('article','title','len_error') ) );	
		}elseif( !valid_str_len($data['content'],10000) ){
			return $this->error( lang( array('article','content') ) );	
		}
		return true;
	}

	public function add_art($data){
		if( !$this->_valid($data) ){
			return false;
		}
		return $this->value($data)->add();	
	}
	
	public function get_art($where="", $limit="", $order="addtime DESC"){
		$list = $this->where($where)->limit($limit)->order($order)->get_view('article');
		return $list;
	}

	public function count_art($where="", $limit=""){
		return $this->where($where)->limit($limit)->count();
	}

	public function edit_art($id,$data){
		if( !$this->_valid($data) ){
			return false;
		}
		$id = intval($id);
		return $this->where("id=$id")->value($data)->update();
	}

	public function del_art($id){
		$id = intval($id);
		return $this->where("id=$id")->del();
	}
}
